import { result } from 'lodash';
/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-08-13 10:00:47
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-06 10:52:09
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
import { BomChangeContentDto, BomChangeServiceProxy, BomChangePublicMethodServiceProxy, CodeFormatDescServiceProxy, CommonServiceProxy } from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { BomMaintainComponent } from './bom-maintain/bom-maintain.component';
import { BomApprovalComponent } from '../bom-approval/bom-approval.component';

@Component({
    selector: 'app-bom-add-or-edit',
    templateUrl: './bom-add-or-edit.component.html',
    styleUrls: ['./bom-add-or-edit.component.less']
})
export class BomAddOrEditComponent extends AppComponentBase {
    @ViewChild('BomApprovalComponent') BomApprovalComponent: BomApprovalComponent;
    @ViewChild('bomMaintain') bomMaintain: BomMaintainComponent;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() editData: any;  //行数据
    @Input() title: string;  //标题
    saving = false;
    active = false;
    form = new BomChangeContentDto();
    codeFormatId = undefined;
    maintTitle = undefined;
    xjOption = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    reasonOption = [
        {
            label: '请选择',
            value: undefined
        }, {
            label: '设计变更',
            value: '设计变更'
        }, {
            label: '工艺变更',
            value: '工艺变更'
        }
    ];
    modelOption = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    lotNoOption = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    btOption = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    changeNoOption = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    saveFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _bomChangeServiceProxy: BomChangeServiceProxy,
        private _bomChangePublicMethodServiceProxy: BomChangePublicMethodServiceProxy,
        private _codeFormatDescServiceProxy: CodeFormatDescServiceProxy,
        private _commonServiceProxy: CommonServiceProxy,
    ) {
        super(injector);
    }

    ngOnInit() {
        //获取型号下拉
        this.modelOption = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._commonServiceProxy.getModelList()
            .pipe(finalize(() => { }))
            .subscribe(result => {
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item.label;
                    data.value = item.value;
                    this.modelOption.push(data);
                }
            });
        // 获取编码规则
        this.changeNoOption = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._codeFormatDescServiceProxy.getCodeFormatDropList()
            .pipe(finalize(() => { }))
            .subscribe(result => {
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item.codeName;
                    data.value = item.id;
                    this.changeNoOption.push(data);
                }
            });
    }
    getLotNo(event) {
        //获取发次
        this.lotNoOption = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._commonServiceProxy.getLotNoList(this.form.moduleId)
            .pipe(finalize(() => { }))
            .subscribe(result => {
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item.label;
                    data.value = item.value;
                    this.lotNoOption.push(data);
                }
            });
    }
    //打开弹窗
    onShown(): void {
        this.active = true;
    }
    //查看新增任务
    show(): void {
        if (this.title == '变更修改' || this.title == '变更查看') {
            this.initEditInfo();//初始化数据
        } else {

        }
        this.active = true;
        this.modal.show();
    }
    initEditInfo() {
        abp.ui.setBusy();
        this.form.updateBTid = this.editData.updateBTid;
        this.form.module = this.editData.module;
        this.form.moduleId = this.editData.moduleId;
        //获取发次
        this.lotNoOption = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._commonServiceProxy.getLotNoList(this.form.moduleId)
            .pipe(finalize(() => { }))
            .subscribe(result => {
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item.label;
                    data.value = item.value;
                    this.lotNoOption.push(data);
                }
                this.form.sendTime = this.editData.sendTime;
                this.form.sendTimeId = this.editData.sendTimeId;
                //获取芯级
                this.xjOption = [
                    {
                        label: '请选择',
                        value: undefined
                    }
                ];
                this._commonServiceProxy.getBomXj(
                    this.editData.moduleId,
                    this.editData.sendTimeId,
                    undefined,
                    undefined
                ).pipe(finalize(() => { }))
                    .subscribe(result => {
                        for (const item of result) {
                            let data = { label: undefined, value: undefined };
                            data.label = item.label;
                            data.value = item.value;
                            this.xjOption.push(data);
                        }
                        this.form.xjId = this.editData.xjId;
                        this.form.xjName = this.editData.xjName;
                        // 获取布套
                        this.btOption = [
                            {
                                label: '请选择',
                                value: undefined
                            }
                        ];
                        this._commonServiceProxy.getBomBt(
                            this.editData.moduleId,
                            this.editData.sendTimeId,
                            undefined,
                            this.form.xjId
                        ).pipe(finalize(() => { }))
                            .subscribe(result => {
                                for (const item of result) {
                                    let data = { label: undefined, value: undefined };
                                    data.label = item.label;
                                    data.value = item.value;
                                    this.btOption.push(data);
                                };
                                this.form.btId = this.editData.btId;
                                abp.ui.clearBusy();
                            });
                    });
            });
        this.form.id = this.editData.id;
        this.form.beforeVersion = this.editData.laterVersion;
        this.form.reasonsChange = this.editData.reasonsChange;
        this.form.reasonsChangeExplain = this.editData.reasonsChangeExplain;
        this.codeFormatId = this.editData.codeId;
        this.form.changeContent = this.editData.changeContent;
    }
    backToApproval() {
        this.modalSave.emit();
        this.close();
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.form = new BomChangeContentDto();
        this.active = false;
        this.modal.hide();
    }
    toMaintain() {
        if (this.form.btId == undefined) {
            this._messageService.error("请先选择BOM信息再进行维护");
            return;
        }
        if (this.title == '变更查看') {
            this.maintTitle = 'BOM查看';
        } else {
            this.maintTitle = 'BOM维护';
        }
        setTimeout(() => {
            this.bomMaintain.show();
        }, 10);
    }
    backMaintain(event) {
        this.form.updateBTid = event[0].data.id;
    }
    saveToapproval() {
        for (const item of this.btOption) {
            if (this.form.btId == item.value) {
                this.form.btName = item.label;
            }
        };
        for (const item of this.xjOption) {
            if (this.form.xjId == item.value) {
                this.form.xjName = item.label;
            }
        }
        for (const item of this.modelOption) {
            if (item.value == this.form.moduleId) {
                this.form.module = item.label;
            }
        }
        for (const item of this.lotNoOption) {
            if (item.label == this.form.sendTimeId) {
                this.form.sendTime = item.label;
            }
        }
        if (this.title == '变更修改') {
            this.form.status = this.editData.status;
        }
        //保存送审
        setTimeout(() => {
            this.BomApprovalComponent.show();
        }, 10);
    }
    save() {
        //保存
        for (const item of this.btOption) {
            if (this.form.btId == item.value) {
                this.form.btName = item.label;
            }
        };
        for (const item of this.xjOption) {
            if (this.form.xjId == item.value) {
                this.form.xjName = item.label;
            }
        }
        for (const item of this.lotNoOption) {
            if (this.form.sendTimeId == item.value) {
                this.form.sendTime = item.label;
            }
        }
        for (const item of this.modelOption) {
            if (this.form.moduleId == item.value) {
                this.form.module = item.label;
            }
        }
        if (this.saveFlag) {
            this.saveFlag = false;
            if (this.title == "变更申请") {
                this._bomChangeServiceProxy.addToBomChangeContent(this.form, this.codeFormatId)
                    .pipe(finalize(() => { this.saving = false; }))
                    .subscribe((result) => {
                        this.saveFlag = true;
                        if (result.code == 0) {
                            this._messageService.success('保存成功');
                            this.modalSave.emit();
                            this.close();
                        } else {
                            this._messageService.error(result.message)
                        }

                    });
            } else if (this.title == "变更修改") {
                this._bomChangeServiceProxy.updateBomChange(this.form)
                    .pipe(finalize(() => { this.saving = false; }))
                    .subscribe((result) => {
                        this.saveFlag = true;
                        if (result.code == 0) {
                            this._messageService.success('保存成功');
                            this.modalSave.emit();
                            this.close();
                        } else {
                            this._messageService.error(result.message)
                        }

                    });
            }

        }
    }
    onChange(event) {
        if (this.form.moduleId != undefined && this.form.sendTimeId != undefined) {
            //获取芯级
            this.xjOption = [
                {
                    label: '请选择',
                    value: undefined
                }
            ];
            this._commonServiceProxy.getBomXj(
                this.form.moduleId,
                this.form.sendTimeId,
                undefined,
                undefined)
                .pipe(finalize(() => { }))
                .subscribe(result => {
                    for (const item of result) {
                        let data = { label: undefined, value: undefined };
                        data.label = item.label;
                        data.value = item.value;
                        this.xjOption.push(data);
                    }
                });
            //获取版本
            this._bomChangeServiceProxy.getVersion(
                undefined,
                this.form.moduleId,
                undefined,
                this.form.sendTimeId,
                undefined
            ).pipe(finalize(() => { }))
                .subscribe(result => {
                    this.form.beforeVersion = result;
                });
        } else {
            this.xjOption = [
                {
                    label: '请选择',
                    value: undefined
                }
            ];
            this.form.beforeVersion = undefined;
        }

    }
    getBt(event) {
        if (this.form.xjId != undefined) {
            this.btOption = [
                {
                    label: '请选择',
                    value: undefined
                }
            ];
            for (const item of this.xjOption) {
                if (this.form.xjId == item.value) {
                    this.form.xjName = item.label;
                }
            }
            this._commonServiceProxy.getBomBtFb(
                this.form.moduleId,
                this.form.sendTimeId,
                undefined,
                this.form.xjId
            ).pipe(finalize(() => { }))
                .subscribe(result => {
                    for (const item of result) {
                        let data = { label: undefined, value: undefined };
                        data.label = item.label;
                        data.value = item.value;
                        this.btOption.push(data);
                    }
                });
        } else {
            this.btOption = [
                {
                    label: '请选择',
                    value: undefined
                }
            ];
        }
    }
}

